package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class ChatClient {
	public static void main(String[] args) {
		Socket s = null;
		PrintWriter out = null;
		try {
			s = new Socket("127.0.0.1", 8888);
			// s = new Socket("172.17.4.74",9999);
			System.out.println("已经与服务器建立连接......");
			Thread chat = new Thread(new ChatHandler(s));
			chat.start();
			out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			while (true) {
				String input = prompt(">");
				out.println(input);
				out.flush();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				s.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @param message
	 * @return tpye
	 */
	private static String prompt(String message) {
		String input = "";
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		System.out.println(message);
		try {
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return input;
	}

}
